<?php


namespace models\Cache\Redis;


use models\Exception\Cache\RedisListException;
use Yaf\Registry;

class RedisListManager
{
    /**
     * 单例实例
     */
    static public $instance = null;

    public $redis_manager = null;

    protected $_client = null;

    protected $db = null;

    /**
     * @param RedisManager $redisManager
     * @return RedisSortedSetManager|null
     */
    static public function getInstance(/*$options = []*/RedisManager $redisManager){
        if(is_null(self::$instance)){
            self::$instance = new self($redisManager);
        }
        return self::$instance;
    }

    public function __construct(/*$options = []*/RedisManager $redisManager){
        $this->redis_manager = $redisManager;
        $this->db = isset(Registry::get('config')['redis']['list']['db']) ? Registry::get('config')['redis']['list']['db'] : 2;
        $this->_client = $redisManager->getClient();
    }

    public function check() {
        try {
            $this->_client->ping();
            $this->_client->select($this->db);
        } catch (\Exception $e) {

        } finally {
            $this->_client->auth($this->_client->getAuth());
        }
    }

    public function getLen($key) {
        if (empty($key))
            throw new RedisListException(RedisListException::REDIS_KEY_IS_NOT_EMPTY_NO, RedisListException::REDIS_KEY_IS_NOT_EMPTY_NO);

        $this->_client->select($this->db);

        return $this->_client->lLen($key);
    }

    public function getList($key, $start = 0, $end = -1) {
        if (empty($key))
            throw new RedisListException(RedisListException::REDIS_KEY_IS_NOT_EMPTY_NO, RedisListException::REDIS_KEY_IS_NOT_EMPTY_NO);

        $this->_client->select($this->db);

        $return_arr = $this->_client->lRange($key, $start, $end);


        foreach ($return_arr as $idx => $val)
            if (null !== $data = json_decode($val, true))
                $return_arr[$idx] = $data;

        return $return_arr;
    }

    public function genList($key, $value, $expire = 86400) {
        if (empty($key))
            throw new RedisListException(RedisListException::REDIS_KEY_IS_NOT_EMPTY_NO, RedisListException::REDIS_KEY_IS_NOT_EMPTY_NO);

        if (!empty($value)) {

            $this->_client->select($this->db);

            $this->_client->watch($key);
            // 开启pipeline
            $this->_client->pipeline();

            if (is_array($value)) {
                foreach ($value as $arr) {
                    if (is_array($arr)) {
                        $this->_client->rPush($key, json_encode($arr));
                    }
                    else
                        $this->_client->rPush($key, $arr);
                }
            }
            $this->_client->expire($key, $expire);

            $this->_client->unwatch();
            return $this->_client->exec();
        }

        return false;
    }

    /**
     * @param $key
     * @return int
     * @throws RedisListException
     */
    public function deleteList($key) {
        if (empty($key))
            throw new RedisListException(RedisListException::REDIS_KEY_IS_NOT_EMPTY_NO, RedisListException::REDIS_KEY_IS_NOT_EMPTY_NO);

        $this->_client->select($this->db);

        return $this->_client->del($key);
    }


}
